package com.ruoyi.web.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.properties.RsaKeyProperties;
import com.ruoyi.framework.util.RsaUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.mobile.company.domain.vo.CompanyListVO;
import com.ruoyi.mobile.company.domain.vo.CompanySearchVO;
import com.ruoyi.mobile.user.domain.vo.UserListVO;
import com.ruoyi.mobile.user.domain.vo.UserSearchVO;
import com.ruoyi.system.domain.SysUserAddDto;
import com.ruoyi.system.domain.SysUserSelectDto;
import com.ruoyi.system.domain.SysUserUpdateDto;
import com.ruoyi.system.domain.SysUserUpdatePwdDto;
import com.ruoyi.system.dto.register.RegisterDto;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.MobileUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.security.PrivateKey;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/h5/user")
@Api(tags = "【新】前端用户管理")
public class SysMobileUserController extends BaseController {
    @Autowired
    private MobileUserServiceImpl userService;

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/user_list")
    @ApiOperation(value = "用户列表")
    public TableDataInfo user_list(UserSearchVO userSearchVO) {
        List<UserListVO> list = userService.selectUserList(userSearchVO);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize();
        List<UserListVO> sysUsers= list.stream().
                sorted(Comparator.comparing(UserListVO::getCreateTime).reversed()).
                skip((long) (pageNum - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(sysUsers);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }


    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/company_list")
    @ApiOperation(value = "企业用户列表")
    public TableDataInfo company_list(CompanySearchVO companySearchVO) {
        List<CompanyListVO> list = userService.selectCompanyUserList(companySearchVO);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        int pageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize();
        List<CompanyListVO> sysUsers= list.stream().
                sorted(Comparator.comparing(CompanyListVO::getCreateTime).reversed()).
                skip((long) (pageNum - 1) * pageSize).limit(pageSize).
                collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(sysUsers);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

}
